package com.sanpu.kxe.business.repay.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.repay.bean.RepayCreditOutput;
import com.sanpu.kxe.business.repay.interfaces.IRepayCreditListener;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：信用卡列表adapter
 * Created by 薛勇军 on 2019/4/10.
 */

public class RepayCreditAdapter extends BaseAdapter {

    private List<RepayCreditOutput.RepayCredit> datas = new ArrayList<>();

    private Context mContext;

    private IRepayCreditListener repayCreditListener;

    public RepayCreditAdapter(List<RepayCreditOutput.RepayCredit> datas, Context mContext,
                              IRepayCreditListener repayCreditListener) {
        this.datas = datas;
        this.mContext = mContext;
        this.repayCreditListener = repayCreditListener;
    }

    @Override
    public int getCount() {
        if (datas == null)
            return 0;
        return datas.size();
    }

    @Override
    public RepayCreditOutput.RepayCredit getItem(int position) {
        if (datas == null)
            return null;
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_repay_credit, null);
            viewHolder.itemView = ButterKnife.findById(convertView, R.id.item_view);
            viewHolder.bankIcon = ButterKnife.findById(convertView, R.id.bankIcon);
            viewHolder.cardNameTv = ButterKnife.findById(convertView, R.id.cardNameTv);
            viewHolder.nameTv = ButterKnife.findById(convertView, R.id.nameTv);
            viewHolder.numberTv = ButterKnife.findById(convertView, R.id.numberTv);
            viewHolder.deleteIv = ButterKnife.findById(convertView, R.id.deleteIv);
            viewHolder.billDayTv = ButterKnife.findById(convertView, R.id.billDayTv);
            viewHolder.refundDayTv = ButterKnife.findById(convertView, R.id.refundDayTv);
            viewHolder.queryIv = ButterKnife.findById(convertView, R.id.queryIv);
            viewHolder.refundIv = ButterKnife.findById(convertView, R.id.refundIv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        RepayCreditOutput.RepayCredit retDatBean = getItem(position);
        if (retDatBean != null) {
            viewHolder.cardNameTv.setText(retDatBean.getBankName());
            try {
                viewHolder.nameTv.setText(retDatBean.getAccountName());
                String bankNo = retDatBean.getCardNo();
                viewHolder.numberTv.setText("尾号" + bankNo.substring(bankNo.length() - 4) + "");
                JKImageLoader.getInstance().displayImage(mContext,
                        UrlConstants.getBaseUrl() + retDatBean.getLogoPath(), viewHolder.bankIcon);

                int[] colors = {Color.parseColor("#"+retDatBean.getCardColorStart()), Color.parseColor("#"+retDatBean.getCardColorEnd())};
                GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
                drawable.setCornerRadius(ScreenUtil.dp2px(3));
                drawable.setShape(GradientDrawable.RECTANGLE);
                drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
                viewHolder.itemView.setBackground(drawable);
//                int resId = mContext.getResources().getIdentifier(
//                        "shape_bank_" + getItem(position).getBankNo(), "drawable", mContext.getPackageName());
//                viewHolder.itemView.setBackgroundResource(resId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!TextUtils.isEmpty(retDatBean.getBillDay())) {
                viewHolder.billDayTv.setText("账单日：每月" + retDatBean.getBillDay() + "号");
            } else {
                viewHolder.billDayTv.setText("账单日：");
            }
            if (!TextUtils.isEmpty(retDatBean.getDueDay())) {
                viewHolder.refundDayTv.setText("还款日：每月" + retDatBean.getDueDay() + "号");
            } else {
                viewHolder.refundDayTv.setText("还款日：");
            }
            viewHolder.deleteIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    repayCreditListener.onDeleteBank(getItem(position));
                }
            });
            viewHolder.queryIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    repayCreditListener.onQueryClick(getItem(position));
                }
            });
            viewHolder.refundIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    repayCreditListener.onRefundClick(getItem(position));
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        private LinearLayout itemView;   //item项
        private ImageView bankIcon;   //银行icon
        private TextView cardNameTv; //银行名称
        private TextView nameTv;     //名字
        private TextView numberTv;   //尾号
        private ImageView deleteIv;   //删除按钮
        private TextView billDayTv;  //账单日
        private TextView refundDayTv;//还款日
        private TextView queryIv;   //查询按钮
        private TextView refundIv;  //还款按钮
    }

    public void setNotifyDataChanged(List<RepayCreditOutput.RepayCredit> data) {
        if (datas == null)
            datas = new ArrayList<>();
        datas.clear();
        if (data != null)
            datas.addAll(data);
        this.notifyDataSetChanged();
    }
}
